﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// Simple Service Administration Tool for WinNT based systems.
// Copyright © 2010 - Silvan Gehrig
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
// Author:
//  Silvan Gehrig
//
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SSATool.UI.View.Bootstrap;
using SSATool.UI.ViewModel;

#endregion

namespace SSATool.UI.View
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the underlying bootstrap context.
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public BootstrapContext BootstrapContext { get; private set; }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow"/> class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            /**
             * Change naming context of context menu, otherwise the
             * context menu cannot be bound with the ElementName binding.
             */
            NameScope.SetNameScope(_ctxNotifyIcons, NameScope.GetNameScope(this)); 
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow"/> class.
        /// </summary>
        /// <param name="bootstrapContext">Specifies the bootstrap context.</param>
        public MainWindow(BootstrapContext bootstrapContext) : this()
        {
            BootstrapContext = bootstrapContext;
            DataContext = bootstrapContext.DataContext;
            bootstrapContext.DataContext.Services.ChildChanged += OnServiceChanged;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Window.Closing"/> event.
        /// </summary>
        /// <param name="e">A <see cref="T:System.ComponentModel.CancelEventArgs"/> that contains the event data.</param>
        protected override void OnClosing(CancelEventArgs e)
        {
            //clean up notifyicon (would otherwise stay open until application finishes)
            _tbiNotifyIcon.Dispose();

            base.OnClosing(e);
        }

        private void OnConnectButtonClick(object sender, RoutedEventArgs e)
        {
            _btnConnect.IsEnabled = false;

            BootstrapContext.DataContext.Connection.Connect(
                _tbxComputer.Text,
                _tbxUserName.Text,
                _tbxPassword.Password,
                OnConnectCallback );
        }

        private void OnConnectCallback(Exception error)
        {
            if (error != null)
            {
                _lblError.Content = error.Message;
                _lblError.ToolTip = error.Message;
                _btnConnect.IsEnabled = true;
            }
            else
            {
                _lblError.Content = string.Empty;
                _lblError.ToolTip = string.Empty;
            }
        }

        private void OnContinueButtonClick(object sender, RoutedEventArgs e)
        {
            if ((_dgdServices.SelectedItem as DataService) != null)
            {
                ((DataService)_dgdServices.SelectedItem).Continue();
            }
        }

        private void OnPauseButtonClick(object sender, RoutedEventArgs e)
        {
            if ((_dgdServices.SelectedItem as DataService) != null)
            {
                ((DataService)_dgdServices.SelectedItem).Pause();
            }
        }

        private void OnStartButtonClick(object sender, RoutedEventArgs e)
        {
            if ((_dgdServices.SelectedItem as DataService) != null)
            {
                ((DataService)_dgdServices.SelectedItem).Start();
            }
        }

        private void OnStopButtonClick(object sender, RoutedEventArgs e)
        {
            if ((_dgdServices.SelectedItem as DataService) != null)
            {
                ((DataService)_dgdServices.SelectedItem).Stop();
            }
        }

        private void OnServiceChanged(object sender, PropertyChangedEventArgs e)
        {
            if (sender != null && sender.Equals(_dgdServices.SelectedItem))
            {
                _dgdServices.SelectedItem = null;
                _dgdServices.SelectedItem = sender;
            }
        }

        private void OnWindowStateChanged(object sender, EventArgs e)
        {
            if (WindowState == WindowState.Minimized)
                Hide();
        }

        private void OnApplicationClose(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void OnApplicationShow(object sender, RoutedEventArgs e)
        {
            Show();
            WindowState = WindowState.Normal;
        }

        #endregion


    }
}
